//
//  UITableView+ReloadData.m
//  TenMinDemo
//
//  Created by Shawn Li on 2021/3/16.
//  Copyright © 2021 CYXiang. All rights reserved.
//

#import "UITableView+ReloadData.h"
#import "TableEmptyView.h"
#import <objc/runtime.h>

@implementation UITableView (ReloadData)

static NSString *isNeedEmptyKey = @"isNeedEmptyKey";
static NSString *emptyViewKey = @"emptyViewKey";


+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method originalMethod = class_getInstanceMethod([self class], @selector(reloadData));
        Method swizzlingMethod = class_getInstanceMethod([self class], @selector(reloadData_checkEmpty));
        BOOL isAdded = class_addMethod([self class], method_getName(originalMethod), method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
//添加不存在的方法，返回Yes；
        if (isAdded)
        {
            NSLog(@"方法在原类中不存在，已经添加成功，用下面的方法替换其实现");
            class_replaceMethod([self class], method_getName(originalMethod), method_getImplementation(swizzlingMethod), method_getTypeEncoding(swizzlingMethod));
        }
        else
        {
            NSLog(@"方法在原类中存在，用下面的方法交换其实现方法");
            method_exchangeImplementations(originalMethod, swizzlingMethod);
        }
        
        NSLog(@"end");
    });
    
}
- (void)originalMethod{
    NSLog(@"原类中的方法");
}


- (void)reloadData_checkEmpty{
    NSLog(@"新的刷新方法");
  
//    先让列表显示
    [self performSelector:@selector(checkEmpty) withObject:nil afterDelay: 0.05];
    
    [self reloadData_checkEmpty];
}

- (void)checkEmpty{
    if(self.isNeedEmpty && [self.isNeedEmpty isEqualToString:@"1"]){
        NSInteger section = [self numberOfSections];
        BOOL isEmpty = YES;
        for (NSInteger i = 0; i<section; i++) {
            NSInteger row = [self numberOfRowsInSection:i];
            if (row>0) {
                isEmpty = false;
                break;
            }
        }
        if (isEmpty) {
            CGFloat width = 100;
            CGFloat height = 122;
            CGFloat x = (self.frame.size.width - width)/2.0;
            CGFloat y = (self.frame.size.height - height)/2.0;
//            if (!self.emptyView) {
            UIView *view = [self viewWithTag:95270];
            if (!view) {
                
                view = [[TableEmptyView alloc] initWithFrame:CGRectMake(x, y, width, height)];
//                view.backgroundColor = [UIColor redColor];
                view.tag = 95270;
                
            }
                
            [self addSubview:view];
        }else{
            UIView *view = [self viewWithTag:95270];
            if (view) {
//                view.backgroundColor = [UIColor greenColor];
                [view removeFromSuperview];
                view = nil;
            }
        }
    }
}


- (NSString *)isNeedEmpty{
    return objc_getAssociatedObject(self, &isNeedEmptyKey);
}

- (void)setIsNeedEmpty:(NSString *)isNeedEmpty{
    objc_setAssociatedObject(self, &isNeedEmptyKey, isNeedEmpty, OBJC_ASSOCIATION_COPY);
}

@end
